package com.cpe.service.member;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.cpe.data.domain.member.Member;
import com.cpe.data.domain.member.MemberForm;
import com.cpe.repository.mapper.member.MemberMapper;
import com.google.common.base.Objects;

import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@Transactional
public class MemberService {

	@Resource
	private MemberMapper memberMapper;

	/**
	 * 更新登录者信息，返回修改后的实体
	 * 
	 * @param loginer
	 * @param memberForm
	 */
	public Member updateLoginer(Member loginer, MemberForm form) throws Exception {
		Assert.notNull(loginer);
		Assert.notNull(form);
		// 不可置空
		if (StringUtil.isNotBlank(form.getNickName()) && !Objects.equal(loginer.getNickName(), form.getNickName())) {
			loginer.setNickName(form.getNickName());
		}
		if (StringUtil.isNotBlank(form.getMobile()) && !Objects.equal(loginer.getMobile(), form.getMobile())) {
			loginer.setMobile(form.getMobile());
		}
		if (StringUtil.isNotBlank(form.getRealName()) && !Objects.equal(loginer.getRealName(), form.getRealName())) {
			loginer.setRealName(form.getRealName());
		}
		if (form.getShowName() != null && !Objects.equal(loginer.getShowName(), form.getShowName())) {
			loginer.setShowName(form.getShowName());
		}

		// 可置空
		if (!Objects.equal(loginer.getWorkingPlace(), form.getWorkingPlace())) {
			loginer.setWorkingPlace(form.getWorkingPlace());
		}
		if (!Objects.equal(loginer.getAddress(), form.getAddress())) {
			loginer.setAddress(form.getAddress());
		}
		if (!Objects.equal(loginer.getPhone(), form.getPhone())) {
			loginer.setPhone(form.getPhone());
		}
		if (!Objects.equal(loginer.getEmail(), form.getEmail())) {
			loginer.setEmail(form.getEmail());
		}
		log.debug("loginer = " + loginer);
		memberMapper.updateById(loginer);
		return loginer;
	}
}
